package image

import (
	"encoding/json"
	"fmt"
	"os"
	"path/filepath"
)

// Manifest is the image manifest struct
type Manifest struct {
	Config   string   `json:"Config,omitempty"`
	Layers   []string `json:"Layers,omitempty"`
	RepoTags []string `json:"RepoTags,omitempty"`
}

func createManifest(tempDir, configFile string, layerFiles, repoTags []string) error {
	var manifestArray []Manifest
	path := filepath.Join(tempDir, "manifest.json")
	out, err := os.Create(path)
	if err != nil {
		return fmt.Errorf("create manifest JSON failed")
	}
	defer out.Close()

	m := Manifest{
		Config:   configFile,
		Layers:   layerFiles,
		RepoTags: repoTags,
	}
	manifestArray = append(manifestArray, m)
	mJSON, err := json.Marshal(manifestArray)
	if err != nil {
		return fmt.Errorf("marshalling manifest JSON failed")
	}

	_, err = out.Write(mJSON)
	if err != nil {
		return fmt.Errorf("writing manifest JSON failed")
	}

	return nil
}
